package com.dc.system.serviceImpl;

import com.dc.common.model.BaseModel;
import com.dc.system.mapper.LogMapper;
import com.dc.system.model.SysLoginlog;
import com.dc.system.model.SysOperationlog;
import com.dc.system.service.LogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Override
    public BaseModel getLoginlogs(BaseModel baseModel, String startTime, String endTime) throws Exception {
        //验证数据，处理数据
        Map map = new HashMap();
        if (baseModel.getKey() != null && !"".equals(baseModel.getKey().trim())) {
            map.put("key", baseModel.getKey().trim());
        } else {
            map.put("key", null);
        }
        if (startTime != null && !"".equals(startTime)) {
            map.put("startTime", startTime);
        } else {
            map.put("startTime", null);
        }
        if (endTime != null && !"".equals(endTime)) {
            map.put("endTime", endTime);
        } else {
            map.put("endTime", null);
        }
        //查询
        PageHelper.startPage(baseModel.getPage() <= 0 ? 1 : baseModel.getPage(), baseModel.getRows() <= 0 ? 10 : baseModel.getRows());
        List<SysLoginlog> logList = logMapper.getLoginlogs(map);
        PageInfo<SysLoginlog> resultlogList = new PageInfo<>(logList);
        baseModel.setData(resultlogList);
        return baseModel;
    }

    @Override
    public BaseModel insertOperationLog(BaseModel baseModel, SysOperationlog systemOperationLog) throws Exception{
        int lines = logMapper.insertOperationLog(systemOperationLog);
        return baseModel;
    }

    @Override
    public BaseModel getOperationlogs(BaseModel baseModel, String startTime, String endTime) throws Exception {
        //验证数据，处理数据
        Map map = new HashMap();
        if (baseModel.getKey() != null && !"".equals(baseModel.getKey().trim())) {
            map.put("key", baseModel.getKey().trim());
        } else {
            map.put("key", null);
        }
        if (startTime != null && !"".equals(startTime)) {
            map.put("startTime", startTime);
        } else {
            map.put("startTime", null);
        }
        if (endTime != null && !"".equals(endTime)) {
            map.put("endTime", endTime);
        } else {
            map.put("endTime", null);
        }
        //查询
        PageHelper.startPage(baseModel.getPage() <= 0 ? 1 : baseModel.getPage(), baseModel.getRows() <= 0 ? 10 : baseModel.getRows());
        List<SysOperationlog> logList = logMapper.getOperationlogs(map);
        PageInfo<SysOperationlog> resultlogList = new PageInfo<>(logList);
        baseModel.setData(resultlogList);
        return baseModel;
    }
}
